package edu.whut.mall.portal.controller;

import edu.whut.mall.common.api.CommonResult;
import edu.whut.mall.portal.domain.ShippingParams;
import edu.whut.mall.portal.service.IShippingService;
import edu.whut.mall.portal.vo.ShippingVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RequestMapping("/shipping")
@RestController
@Api(value="收货地址管理器",tags = {"收货地址管理器"})
public class RestShippingController {

    private final IShippingService shippingService;

    @ApiOperation("添加收货地址")
    @PostMapping(value = "/addOneShipping")
    public CommonResult addOneShipping(@Valid @RequestBody ShippingParams shippingParams) {
        int result = shippingService.insertOneShipping(shippingParams);
        if (result <= 0) {
            return CommonResult.failed("添加收货地址失败");
        }
        return CommonResult.success("添加收货地址成功");
    }

    @ApiOperation("更新收货地址")
    @PostMapping(value = "/updateOneShipping")
    public CommonResult updateOneShipping(@RequestBody ShippingParams shippingParams) {
        int result = shippingService.updateOneShipping(shippingParams);
        if (result <= 0) {
            return CommonResult.failed("更新收货地址失败");
        }
        return CommonResult.success("更新收货地址成功");
    }

    @ApiOperation("根据用户id显示所有的收货地址")
    @GetMapping(value = "/listShippingVosByUserId")
    public CommonResult listShippingVosByUserId(@NotNull @RequestParam Long userId) {
        List<ShippingVo> shippingVoList = shippingService.listShippingVosByUserId(userId);
        if (shippingVoList ==null) {
            return CommonResult.failed("暂时没有收货地址");
        }
        return CommonResult.success(shippingVoList,"获取收货地址列表成功");
    }

    @ApiOperation("根据shippingId删除收货地址")
    @PostMapping(value = "/deleteShippingByShippingId")
    public CommonResult deleteShippingByShippingId(@NotNull @RequestParam Long shippingId) {
        int result = shippingService.deleteOneShipping(shippingId);
        if (result == 0) {
            return CommonResult.failed("删除收货地址失败");
        }
        return CommonResult.success("删除收货地址成功");
    }

    @ApiOperation("设为默认收货地址")
    @PostMapping(value = "/setDefaultShipping")
    public CommonResult setDefaultShipping(@NotNull @RequestParam Long userId,
                                           @NotNull @RequestParam Long shippingId) {
        int result = shippingService.setDefaultShipping(userId, shippingId);
        if (result <= 0) {
            return CommonResult.failed("设为默认收货地址失败");
        }
        return CommonResult.success("设为默认收货地址成功");
    }


    @ApiOperation("未发货时修改地址")
    @PostMapping(value = "/modifyShippingAddress")
    public CommonResult modifyShippingAddress(@NotNull @RequestParam String orderNo,
                                              @NotNull @RequestParam String username,
                                              @NotNull @RequestParam String phone,
                                              @NotNull @RequestParam String receiveAddress) {
        return CommonResult.success(shippingService.modifyShippingAddress(orderNo, username, phone, receiveAddress), "修改收货地址成功");
    }
}